package com.chenzhen.blog.controller;

/**
 * @author hp
 */



import com.chenzhen.blog.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;


@RestController
@RequestMapping("/file")
public class FileController {


    @PostMapping(value = "/uploadImage")
    public ResponseEntity<String> uploadImage(@RequestParam("image") MultipartFile file) {
        try {
            if (file == null || file.isEmpty()) {
                return ResponseEntity.badRequest().body("File is empty");
            }

            // 处理上传的文件并获取图片地址
            String imageUrl = FileUtils.getFileAddress(file, "demo/images");

            // 返回JSON格式的响应
            System.out.println();
            return ResponseEntity.ok().body("{\"imageUrl\": \"" + imageUrl + "\"}");

        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("Internal Server Error");
        }
    }

}